package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.SellerAddress;
import cn.signalpha.modules.shop.service.SellerAddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/seller/address")
@Api(value = "商户地址", tags = "商户地址")
public class SpiSellerAddressController extends BaseController {

	private final SellerAddressService sellerAddressService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<SellerAddress>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		QueryWrapper<SellerAddress> wrapper = Condition.getQueryWrapper(params, SellerAddress.class);
		wrapper.lambda().eq(SellerAddress::getSellerId, getUser().getTenantId());
		return R.data(sellerAddressService.page(Condition.getPage(query), wrapper));
	}

	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "")
	public R<List<SellerAddress>> list(@ApiIgnore @RequestParam Map<String, Object> params){
		QueryWrapper<SellerAddress> wrapper = Condition.getQueryWrapper(params, SellerAddress.class);
		wrapper.lambda().eq(SellerAddress::getSellerId, getUser().getTenantId());
		return R.data(sellerAddressService.list(wrapper));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<SellerAddress> info(@RequestParam Long id){
		return R.data(sellerAddressService.getById(id));
	}

	@PostMapping("/submit")
	@ApiOperation(value = "提交", notes = "")
	public R submit(@Valid @RequestBody SellerAddress sellerAddress) {
		sellerAddress.setSellerId(Long.valueOf(getUser().getTenantId()));
		if(sellerAddress.getId() != null){
			sellerAddressService.removeCaCheById(sellerAddress.getId());
		}
		return R.status(sellerAddressService.saveOrUpdate(sellerAddress));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			sellerAddressService.removeById(id);
			sellerAddressService.removeCaCheById(id);
		});
		return R.status(true);
	}

}
